package com.ourteam.system.api;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

public class UserCredentialsMatcher extends SimpleCredentialsMatcher {

	@Override
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

		UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;

		SimpleAccount account = (SimpleAccount) info;

		if (account.getCredentials().equals(new String(usernamePasswordToken.getPassword()))) {
			return true;
		} else if (account.getCredentials()
				.equals(DigestUtils.md5Hex(new String(usernamePasswordToken.getPassword())))) {
			return true;
		}

		return false;
	}

}
